package com.asia5b.a5bmall.same.dialog

import android.graphics.Bitmap
import android.view.Gravity
import android.view.ViewGroup
import androidx.lifecycle.Observer
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5bmall.same.databinding.DfImagevcodeBinding
import com.asia5b.a5mall.middleware.base.SimpleDialogFragment
import com.asia5b.a5mall.middleware.config.GlobalValue
import com.asia5b.a5mall.middleware.logic.viewmodel.member.AccoutVM
import com.asia5b.a5mall.middleware.model.bean.CheckSmsBean
import com.asia5b.a5mall.middleware.model.bean.ImageCodeType
import com.asia5b.a5mall.middleware.utils.showMessage
import kotlinx.android.synthetic.main.df_imagevcode.*

/**
 * Created by zhangyuncai on 2020/4/13.
 */
class ImageCodeDF : SimpleDialogFragment<AccoutVM, DfImagevcodeBinding>() {

    override val viewModelClazz: Class<AccoutVM>
        get() = AccoutVM::class.java

    override fun getDataBinding(container: ViewGroup?): DfImagevcodeBinding {
        return DfImagevcodeBinding.inflate(layoutInflater,container,false)
    }
    var imageCodeType: ImageCodeType? = null
    var bean: CheckSmsBean? = null


    override fun initView() {
        GlobalValue.setLength(et_imagevcode_code)
        setCancelable(false)
    }

    override fun initEvent() {
        //取消
        tv_imagevcode_cancel.setOnClickListener {
//            mSureClick?.invoke(null)
            dismiss()
        }
        //确定
        tv_imagevcode_sure.setOnClickListener {
            val text = et_imagevcode_code.text.toString().trim()
            if (text == "") {
                showMessage(ResString.get(com.asia5b.a5mall.middleware.R.string.请输入图片校验码))
                return@setOnClickListener
            }
            mSureClick?.invoke(text)
            dismiss()
        }
    }

    override fun initData() {
        loadImageCode()

        mViewModel.loadImageCodeLiveData.observe(this, Observer {
            loadImageCodeResult(it)
        })
    }

     fun loadImageCodeResult(bitmap: Bitmap) {
        iv_imagevcode_code.setImageBitmap(bitmap)
    }


    /**
     * 加载图片验证码
     */
     fun loadImageCode() {
        if (bean != null) {
            mViewModel.loadImageCode(bean!!)
        } else if (imageCodeType != null) {
            mViewModel.loadImageCode(imageCodeType!!)
        }

    }

    override val gravity: Int
        get() = Gravity.CENTER


    override val isCancelableOutside: Boolean
        get() = false

    override val widthPercent: Double
        get() = 0.7

}